<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="组织列表"
      row-key="_id"
      :pagination="false"
      :indent="20"
      :columns="columns"
      :highlight-current-row="true"
      :request-api="getTableList"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增 </el-button>
        <el-button
          type="primary"
          plain
          :icon="CirclePlus"
          :disabled="!scope.isSelected"
          @click="openDrawer('新增下级', {}, scope.selectedList)"
        >
          新增下级
        </el-button>
      </template>
      <!-- 菜单图标 -->
      <template #icon="scope">
        <el-icon :size="18">
          <component :is="scope.row.meta.icon"></component>
        </el-icon>
      </template>
      <!-- 菜单操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)"> 编辑 </el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteData(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <OrgDrawer ref="drawerRef" />
  </div>
</template>

<script setup lang="ts" name="menuMange">
import { computed, onMounted, ref } from "vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { Delete, EditPen, CirclePlus } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import apiService from "@/api/services/apiService";
import OrgDrawer from "./orgDrawer.vue";
import { useHandleData } from "@/hooks/useHandleData";
import { dateRender } from "@/hooks/useColumnRender";
import { Org } from "@/api/types/base";
import { useDataStore } from "@/stores/modules/data";

const proTable = ref();
const dataStore = useDataStore();

const userList = computed(() => dataStore.userList);
onMounted(() => {
  dataStore.findUserList();
});

const getTableList = (params: any) => {
  params.options.sort = {
    sort: 1
  };
  return apiService.findTree("orgs", params.condition, params.options);
};

// 表格配置项
const columns: ColumnProps[] = [
  { type: "index", label: "序号", width: 85 },
  { prop: "name", label: "组织名称", align: "left", search: { el: "input" } },
  {
    prop: "leader",
    label: "负责人",
    // render: scope => userRender(scope, userList),
    enum: userList,
    fieldNames: { label: "userName", value: "_id" }
  },
  {
    prop: "superVisor",
    label: "上级",
    enum: userList,
    // render: scope => userRender(scope, userList),
    fieldNames: { label: "userName", value: "_id" }
  },
  {
    prop: "assistantManage",
    label: "辅助负责人",
    // render: scope => userRender(scope, userList),
    enum: userList,
    fieldNames: { label: "userName", value: "_id" }
  },
  { prop: "sort", label: "排序" },
  { prop: "createDate", label: "创建时间", render: dateRender },
  { prop: "operation", label: "操作", width: 180, fixed: "right" }
];

// 删除用户信息
const deleteData = async (params: Org) => {
  await useHandleData(apiService.deleteById, "orgs", params._id, `删除参数【${params.name}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof OrgDrawer> | null>(null);
const openDrawer = (title: string, row: Partial<Org> = {}, selectList: { [key: string]: any }[] = []) => {
  if (title.indexOf("新增") !== -1) {
    selectList?.length ? (row.pid = selectList[0]._id) : delete row.pid;
  }
  const params = {
    title,
    tbName: "orgs",
    isView: title === "查看",
    row: { ...row },
    api: title.indexOf("新增") !== -1 ? apiService.save : title === "编辑" ? apiService.updateById : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
